import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLInt

} from 'graphql';

import Company from '../../db/models/company'

export let companyType = new GraphQLObjectType({
    name: 'company',
    fields: {
        id: {
            type: GraphQLInt
        },
        company: {
            type: GraphQLString
        }
    }
})

export const companyList = {
    type: new GraphQLList(companyType),
    args: {},
    resolve(root, params, options) {
        return Company.findAll()
    }
}

export const companyOne = {
    type: companyType,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Company.findOne({
            where: {
                id: params.id
            }
        })
    }
}
export const companyUpdate = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Company.update({
            company: params.company
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const companyAdd = {
    type: companyType,
    args: {
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Company.create({
            company: params.company
        })
    }
}

export const companyDel = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Company.destroy({
            where: {
                id: params.id
            }
        })
    }
}
